package cbaApplication;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import operacoes2.Levantamento;
import operacoes2.Operacao;

// <editor-fold defaultstate="collapsed" desc=" UML Marker ">
// #[regen=yes,id=DCE.ABEDC692-9565-DD73-7D31-F81407E31382]
// </editor-fold>
public class Files {

    // <editor-fold defaultstate="collapsed" desc=" UML Marker ">
    // #[regen=yes,id=DCE.0FDF6AC4-E2C8-E38E-3280-613E08E62D5E]
    // </editor-fold>
    private File operacoesFile;
    private File valoresFile;
    private File backupFile;
    ObjectOutputStream out;
    ObjectInputStream in;
    // <editor-fold defaultstate="collapsed" desc=" UML Marker ">
    // #[regen=yes,id=DCE.02BF05E1-D519-ACC0-DE22-B0E5CF569C19]
    // </editor-fold>
    private final String FILE_OPERACOES = "operacoes.dat";
    private final String FILE_VALORES = "valores.dat";
    private final String FILE_BACKUP = "backup.dat";
    private static Files files;

    // <editor-fold defaultstate="collapsed" desc=" UML Marker ">
    // #[regen=yes,id=DCE.43BFFCAC-4D09-272D-451F-2818F935D078]
    // </editor-fold>
    private Files() {
        this.operacoesFile = new File(FILE_OPERACOES);
        this.valoresFile = new File(FILE_VALORES);
        this.backupFile = new File(FILE_BACKUP);
    }

    public static Files getInstance() {
        if (files == null) {
            files = new Files();
        }
        return files;
    }

    public synchronized boolean saveOperacoes(Vector<Operacao> operacoes) {
        try {
            out = new ObjectOutputStream(new FileOutputStream(operacoesFile));
            out.writeObject(operacoes);
            out.close();
            return true;
        } catch (IOException ex) {
            System.out.println("saveOperacoes: error");
            return false;
        }
    }

    public synchronized Vector<Operacao> loadOperacoes() {
        Vector<Operacao> ret = new Vector<Operacao>();
        try {
            in = new ObjectInputStream(new FileInputStream(operacoesFile));
            ret = (Vector<Operacao>) in.readObject();
            in.close();
            return ret;
        } catch (Exception ex) {
            System.out.println("loadOperacoes: file no found");
            saveOperacoes(ret);
            return ret;
        }

    }

    public synchronized boolean saveValores(double saldo, double depositos) {
        try {
            double[] valores = {saldo, depositos};
            out = new ObjectOutputStream(new FileOutputStream(valoresFile));
            out.writeObject(valores);
            out.close();
            return true;
        } catch (IOException ex) {
            System.out.println("saveValores: error");
            return false;
        }
    }

    public synchronized double[] loadValores() {
        double[] ret = {0, 0};
        try {
            in = new ObjectInputStream(new FileInputStream(valoresFile));
            ret = (double[]) in.readObject();
            in.close();

        } catch (Exception ex) {
            saveValores(0, 0);
            System.out.println("loadValores: file no found");
        }
        return ret;
    }

    public synchronized void saveBackup(Vector<Levantamento> oper) {
        try {
            out = new ObjectOutputStream(new FileOutputStream(new File(FILE_BACKUP)));
            out.writeObject(oper);
            out.close();
        } catch (IOException ex) {
            Logger.getLogger(Files.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public synchronized Vector<Levantamento> getBackup() {
        Vector<Levantamento> ret = new Vector<Levantamento>();
        try {
            in = new ObjectInputStream(new FileInputStream(backupFile));
            ret = (Vector<Levantamento>) in.readObject();
            //fileBackup
            //System.out.println("Carregou clientes:" + ret.size());
            in.close();
        } catch (Exception ex) {
            System.out.println("não ha backups");
        }
        return ret;
    }

   public void deleteBackups() {
        if (backupFile.delete()) {
            System.out.println("backups apagados");
        } else {
            System.out.println("falha ao apagar backups");
        }
    }

   public void deleteOperations() {
        if (operacoesFile.delete()) {
            System.out.println("operaçoes apagadas");
        } else {
            System.out.println("falha ao apagar operaçoes");
        }
    }
}




